from functools import wraps
import json
from typing import Any

from auditlog.context import set_actor

from customauth.models import MyUser

FunResult = Any


def login_for_logs_by_mpp(func):  # noqa: ANN201
    """Функция которая принимает другую функцию."""

    @wraps(func)
    def wrapper(*args, **kwargs) -> FunResult:  # noqa: ANN002, ANN003
        """Оберточная функция для логирования от лица пользователя."""
        request = args[0]
        params = json.loads(request.body)
        if request.method == "GET":
            mpp = request.GET.get("mpp")
        if request.method == "POST":
            mpp = params.get("mpp", False)
        if request.method == "DELETE":
            mpp = params.get("mpp", False)
        print(mpp)
        user = MyUser.objects.get(mpp=mpp)
        with set_actor(user):
            return func(*args, **kwargs)
    return wrapper
